﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Galgame_decompression
{
    class Cat_helper
    {
        public static String local_path = System.AppDomain.CurrentDomain.BaseDirectory;
        public static String local_path_temp = System.AppDomain.CurrentDomain.BaseDirectory + "temp\\";
        public static bool Decompression_complete = false;
        public static int Initial_number = 0;
        public static int Final_number = 0;
        public static int ProgressBar_MinNum = 0;
        public static int ProgressBar_MaxNum = 100;
        public static string Unzip_log = "";


        /// <summary>
        /// 点击调用默认方式打开这个路径
        /// </summary>
        /// <param name="File_path"></param>
        public void Folder_open(String File_path)
        {
            if (!Directory.Exists(File_path))
            {
                Console.WriteLine("文件不存在");
                File_existence(File_path);

            }
            System.Diagnostics.Process.Start("explorer.exe", File_path);

        }


        /// <summary>
        ///判断文件是否存在，如果不存在则进行创建 需要文件路径，尽量是完整的，如果不是完整的则会在当前项目创建
        /// </summary>
        /// <param name="file_path">文件路径</param>
        public static void File_existence(String file_path)
        {

            String result = Path.GetExtension(file_path);
            Console.WriteLine("获取到文件名('{0}') 扩展名： '{1}'",
                file_path, result);
            if (result == "" && (!System.IO.Directory.Exists(file_path)))
            {
                try
                {
                    Directory.CreateDirectory(file_path);
                    Console.WriteLine("文件目录不存在，已创建");

                }
                catch (Exception e)
                {
                    Console.WriteLine("创建文件目录异常：" + e);
                }
            }
            else if (result != "" && !System.IO.File.Exists(file_path))
            {
                try
                {
                    System.IO.File.WriteAllText(file_path, string.Empty, Encoding.UTF8);
                    Console.WriteLine("普通文件不存在，已创建");
                }
                catch (Exception e)
                {
                    Console.WriteLine("创建普通文件异常：" + e);
                }

            }
            else
            {
                Console.WriteLine("文件存在，无需创建");
            }

        }



        /// <summary>
        /// 取得某个关键词的值并且读取到list中
        /// </summary>
        /// <param name="dictionary_name">路径名 如.//xxxx//</param>
        /// <param name="file_name">文件名 如cat.txt</param>
        /// <returns></returns>
        public List<String> Get_text_value(String dictionary_name, String file_name,Encoding encode)
        {
            try
            {
                return new List<String>(System.IO.File.ReadAllLines(dictionary_name + file_name, encode));
            }
            catch (Exception e)
            {
                Console.WriteLine("未找到文件" + e);
                File_existence(dictionary_name);
                File_existence(dictionary_name + file_name);
                return new List<string>();
            }
        }

        public void Text_repeat( String file_name) {
            
            HashSet<string> strlist = new HashSet<string>();
            string filename = file_name;
            int repcnt = 0;
            string[] lines = System.IO.File.ReadAllLines(filename, System.Text.Encoding.Default);
            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i] != "")
                {
                    if (!strlist.Add(lines[i]))
                    {
                        repcnt++;
                    }
                }
            }
            if (repcnt > 0)
            {
                Console.WriteLine("过滤重复 " + repcnt);
            }
            Console.WriteLine("导入数量 " + strlist.Count);
            System.IO.File.WriteAllLines(filename, strlist, System.Text.Encoding.UTF8);//覆盖文件重新写入，注意！
        }
    }
}
